<!-- HTML header for doxygen 1.8.13-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.7"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Over The Air (OTA) Update Library: Over The Air(OTA) Library Overview</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
  $(window).load(resizeHeight);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>
<link href="doxygen_style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><a href="http://www.cypress.com/"><img alt="Logo" src="infineon_logo.png"/></a></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">Over The Air (OTA) Update Library</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.7 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li class="current"><a href="index.html"><span>Home</span></a></li>
      <li><a href="modules.html"><span>API&#160;Reference</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('index.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Data Structures</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Groups</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark">&#160;</span>Pages</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">Over The Air(OTA) Library Overview </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>The library provides support for performing over-the-air (OTA) updates on the application code that runs on various devices, including PSoC™ 6 Connectivity Devices, 20829 (CYW920829M2EVK-02) Devices, 89829 (CYW989829M2EVB-01) Devices, XMC7000 (KIT_XMC72_EVK) Devices, and CYW955913SDCM2WLIPA (CYW955913EVK-01) Devices, which can be connected via Wi-Fi, Bluetooth®, or Ethernet.<br />
 For simplification, this document may refer to an MQTT Broker or an HTTP Server as "server".<br />
<br />
 The library provides a number of combinations to get the OTA image:<br />
 </p>
<p><b>1. Over Bluetooth®</b></p><ul>
<li>We use a Push model with Bluetooth®. A Peer application running on a laptop or phone will connect and push an update to the Device.<br />
 </li>
</ul>
<p><b>2. Over Wi-Fi or Ethernet(MQTT/HTTP)</b></p><ul>
<li>MQTT and HTTP use a Pull model. There are two flows for getting the OTA image(JOB flow and Direct flow).<br />
 </li>
</ul>
<p><b>2.1 Pull Model using a JOB flow:</b></p><ul>
<li>Step 1: Get Job:<br />
<ol type="1">
<li>The OTA Agent downloads a JSON "Job" document (default name "ota_update.json").<br />
</li>
<li>The Job document has information about an update that is available, including its location.<br />
</li>
</ol>
</li>
<li>Step 2: Get Data:<br />
<ol type="1">
<li>The OTA Agent will download the OTA image and writes it to the configured upgrade slot using registered "upgrade image handling storage callback APIs".<br />
 </li>
</ol>
</li>
</ul>
<p><b>2.2 Pull Model using a Direct flow:</b></p><ul>
<li>Step 1: Get Data:<br />
<ol type="1">
<li>The OTA Agent will download the OTA image from predefined location and writes it to the configured upgrade slot using registered "upgrade image handling storage callback APIs".<br />
</li>
</ol>
</li>
<li>Choose direct flow by setting the use_get_job_flow field in <a class="el" href="structcy__ota__network__params__t.html" title="OTA Connection parameters structure. ">cy_ota_network_params_t</a> to CY_OTA_DIRECT_FLOW when calling <a class="el" href="group__group__ota__functions.html#ga05b1ce8823a81d534247afd43db4c536" title="Start the OTA Background Agent. ">cy_ota_agent_start()</a>.<br />
 </li>
</ul>
<p>On the next reboot, Bootloader will take care of booting the new version of the application from the configured upgrade slot.<br />
 </p>
<p>The ModusToolbox OTA code examples import this OTA library automatically.</p>
<h1><a class="anchor" id="section_features"></a>
Features and Functionality</h1>
<p>This library utilizes MQTT or HTTPS and TLS, or Bluetooth® to securely connect to and download an update for the users application. Server info, credentials, and other parameters are passed into <a class="el" href="group__group__ota__functions.html#ga05b1ce8823a81d534247afd43db4c536" title="Start the OTA Background Agent. ">cy_ota_agent_start()</a>.</p>
<p><b>Other features:</b></p><ol type="1">
<li>MQTT and HTTP<ul>
<li>Configuration to adjust multiple timing values to customize how often to check for updates, and other parameters for the MQTT Broker connection.</li>
<li>Runs in a separate background thread, only connecting to the server based on the parameters passed to <a class="el" href="group__group__ota__functions.html#ga05b1ce8823a81d534247afd43db4c536" title="Start the OTA Background Agent. ">cy_ota_agent_start()</a>.</li>
<li>Provides a callback mechanism to report stages of connect, download percentage, and errors.</li>
<li>Once the application starts the OTA Agent, the OTA Agent will contact the server at the defined intervals to see if there is an update available.</li>
<li>Upon receiving the upgrade image, OTA Agent gives a callback to the application for handling the upgrade image.</li>
</ul>
</li>
<li>Bluetooth®<ul>
<li>Runs in a separate thread with callbacks to the users application to handle the connection messages and download data.</li>
<li>When Bluetooth® is enabled in the compile flags, the Bluetooth® server will start automatically.</li>
</ul>
</li>
</ol>
<h1><a class="anchor" id="section_notes"></a>
Integration Notes</h1>
<p>Before enabling OTA in an application, the user must choose a suitable bootloader and program it on the chosen platform(s). The required build environment for creating BOOT and UPGRADE images should be set up. To add the required bootloader support at the application level itself, the OTA update library implements a callback mechanism and offloads handling of upgrade images including flash operations to application. The application must implement these storage operation callbacks defined in ota-update/&lt;version&gt;/include/cy_ota_api.h to handle upgrade image based on bootloader choosen and must register callbacks during OTA agent start by giving a parameter of type '<a class="el" href="structcy__ota__storage__interface__t.html" title="OTA storage interface callback APIs structure. ">cy_ota_storage_interface_t</a>' in '<a class="el" href="group__group__ota__functions.html#ga05b1ce8823a81d534247afd43db4c536" title="Start the OTA Background Agent. ">cy_ota_agent_start()</a>'.</p>
<p>A pre-defined configuration file has been bundled with this library and the user is expected to:<br />
</p><ul>
<li>Copy the cy_ota_config.h file from the ota-update/&lt;version&gt;/configs directory to the top-level code example directory.<br />
</li>
<li>Copy FreeRTOSConfig.h configuration file for the CM4(PSoC™ 6), CM33(CYW920829M2EVK-02/CYW989829M2EVB-01)or CM7(KIT_XMC72_EVK) from ota-update/&lt;version&gt;/configs/FreeRTOS directory to the top-level code example directory.<br />
</li>
<li>Provide application version information in application makefile.<br />
</li>
<li>OTA library user is expected to provide application version(X.Y.Z) by adding values for APP_VERSION_MAJOR, APP_VERSION_MINOR, and APP_VERSION_BUILD.<br />
<div class="fragment"><div class="line">APP_VERSION_MAJOR=&lt;X&gt;</div>
</div><!-- fragment --> <div class="fragment"><div class="line">APP_VERSION_MINOR=&lt;Y&gt;</div>
</div><!-- fragment --> <div class="fragment"><div class="line">APP_VERSION_BUILD=&lt;Z&gt;</div>
</div><!-- fragment --> <br />
</li>
<li>Please refer to the ota-update/&lt;version&gt;/OTA_MAKEFILE_INFO_README.md file for more details on OTA application makefile.<br />
</li>
<li>For <b>HTTP support</b>, add the following to the application Makefile:<br />
<div class="fragment"><div class="line">OTA_HTTP_SUPPORT=1</div>
</div><!-- fragment --> <br />
</li>
<li>For <b>MQTT support</b>, add the following to the application Makefile:<br />
<div class="fragment"><div class="line">OTA_MQTT_SUPPORT=1</div>
</div><!-- fragment --> <br />
</li>
<li>For <b>Bluetooth®</b> support, add the following to the application Makefile:<br />
<div class="fragment"><div class="line">OTA_BT_SUPPORT=1</div>
</div><!-- fragment --> <br />
</li>
<li>OTA_MQTT_SUPPORT, OTA_HTTP_SUPPORT and OTA_BT_SUPPORT can be used simultaneously, but will possibly be too large to fit in the FLASH slot available.<br />
</li>
<li>There are other COMPONENTS that may be needed based on support. Please refer to the various README.md files in the root ota-update library directory.<br />
<ol type="1">
<li>README.md<br />
</li>
<li>OTA_MAKEFILE_INFO_README.md<br />
</li>
</ol>
</li>
</ul>
<h1><a class="anchor" id="section_concept"></a>
General Concept</h1>
<p>The OTA Agent downloads upgrade version of OTA applications and stores the update to the upgrade slot using user registered storage callbacks. Once after completing the download, OTA library verifies update image(checksum) and reboots device. When device reboots, programmed bootloader boots upgrade image of application. <br />
 </p>
<p><b> MCUBootloader - The basic device boot sequence is as follows:</b> <br />
</p><ol type="1">
<li>ROM boot will start Bootloader.</li>
<li>Bootloader runs the current application in the BOOT slot.<br />
</li>
<li>The OTA Agent downloads the update and stores it in the UPGRADE slot.<br />
</li>
<li>The OTA Agent marks the upgrade as ready.<br />
<br />
 <b> On the next system boot: </b></li>
</ol>
<ol type="1">
<li>ROM boot will start Bootloader</li>
<li>If no update is downloaded, Bootloader starts the current application.</li>
<li>Bootloader determines if there is an update in the UPGRADE Slot</li>
<li>If there is an update available:<br />
 a. Bootloader verifies the update.<br />
 b. Bootloader starts the new (updated) application.<br />
 c. The updated application must validate the update. <br />
 </li>
</ol>
<p><b> CYW955913EVK-01 kit's in-built Bootloader - The basic device boot sequence is as follows:</b> <br />
</p><ol type="1">
<li>ROM boot will start CYW955913EVK-01 kit's in-built Bootloader.<br />
</li>
<li>In-built Bootloader starts the current application in Active DS.<br />
</li>
<li>If there is an update available in Non Active DS,<br />
 a. In-built Bootloader verifies the update image headers and certificates.<br />
 b. If a valid upgrade image is found, the in-built Bootloader will activate the non-active DS and deactivate the active DS.<br />
 c. On reset, in-built Bootloader starts the application in Active DS. <br />
 </li>
</ol>
<p>For more general information, see the various README.md files in the ota-update/&lt;version&gt;/ directory:  <br />
</p><ul>
<li>README.md<br />
</li>
<li>OTA_MAKEFILE_INFO_README.md<br />
</li>
</ul>
<h1><a class="anchor" id="section_api_overview"></a>
API Overview</h1>
<h2>Start the OTA Agent </h2>
<p><code>cy_ota_start_agent()</code> is a non-blocking call, which returns a context pointer that is used in subsequent calls.<br />
 When using Bluetooth®, <code>cy_ota_start_agent()</code> is called after the Peer sends a PREPARE_DOWNLOAD message.<br />
 When using MQTT or HTTP, the OTA Agent uses callbacks to signal the application when events occur.<br />
 <br />
 <code>cy_rslt_t <a class="el" href="group__group__ota__functions.html#ga05b1ce8823a81d534247afd43db4c536" title="Start the OTA Background Agent. ">cy_ota_agent_start(cy_ota_network_params_t *network_params, cy_ota_agent_params_t *agent_params, cy_ota_storage_interface_t *storage_interface, cy_ota_context_ptr *ctx_ptr)</a>;</code></p>
<p>These defines determine when the checks happen. Between checks, the OTA Agent is not connected to a server, and not checking for updates.<br />
</p><ul>
<li>CY_OTA_INITIAL_CHECK_SECS</li>
<li>CY_OTA_NEXT_CHECK_INTERVAL_SECS</li>
<li>CY_OTA_RETRY_INTERVAL_SECS</li>
<li>CY_OTA_CHECK_TIME_SECS</li>
</ul>
<h2>Stop the OTA Agent </h2>
<p>When you want to stop the OTA Agent.<br />
 <br />
 <code>cy_rslt_t <a class="el" href="group__group__ota__functions.html#gafc2e0386db205ece6b355fa5dd91437a" title="Stop OTA Background Agent. ">cy_ota_agent_stop(cy_ota_context_ptr *ctx_ptr)</a>;</code></p>
<h2>Trigger a check right now. </h2>
<p>Use this when you want to trigger a check for an OTA update earlier than<br />
 is currently scheduled. The OTA Agent must have been started already. For MQTT and HTTP only.<br />
 <br />
 <code>cy_rslt_t <a class="el" href="group__group__ota__functions.html#ga06bc6c2f2eb7cb6f52f80ef16574fd1e" title="Check for OTA update availability and download update now. ">cy_ota_get_update_now(cy_ota_context_ptr ctx_ptr)</a>;</code></p>
<h1><a class="anchor" id="section_override"></a>
Override Default Settings</h1>
<p>For MQTT and HTTP usage, copy the cy_ota_config.h file from the ota-update/&lt;version&gt;/configs<br />
 directory to the top-level code example directory in the project.<br />
 Change these values to your preferred settings.<br />
 </p><div class="fragment"><div class="line"></div>
<div class="line"><span class="preprocessor">#define CY_OTA_INITIAL_CHECK_SECS           (10)            </span><span class="comment">/* 10 seconds */</span><span class="preprocessor"></span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#define CY_OTA_NEXT_CHECK_INTERVAL_SECS     (24 * 60 * 60)  </span><span class="comment">/* 1 day between checks */</span><span class="preprocessor"></span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#define CY_OTA_RETRY_INTERVAL_SECS          (5)             </span><span class="comment">/* 5 seconds between retries after an error */</span><span class="preprocessor"></span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#define CY_OTA_CHECK_TIME_SECS              (10 * 60)       </span><span class="comment">/* 10 minutes */</span><span class="preprocessor"></span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#define CY_OTA_PACKET_INTERVAL_SECS         (0)             </span><span class="comment">/* default disabled */</span><span class="preprocessor"></span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#define CY_OTA_JOB_CHECK_TIME_SECS           (30)               </span><span class="comment">/* 30 seconds */</span><span class="preprocessor"></span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#define CY_OTA_DATA_CHECK_TIME_SECS          (20 * 60)           </span><span class="comment">/* 20 minutes */</span><span class="preprocessor"></span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#define CY_OTA_RETRIES                      (3)             </span><span class="comment">/* retry entire process 3 times */</span><span class="preprocessor"></span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#define CY_OTA_CONNECT_RETRIES              (3)             </span><span class="comment">/* 3 server connect retries  */</span><span class="preprocessor"></span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#define CY_OTA_MAX_DOWNLOAD_TRIES           (3)             </span><span class="comment">/* 3 download OTA Image retries */</span><span class="preprocessor"></span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#define CY_OTA_HTTP_TIMEOUT_SEND            (3000)         </span><span class="comment">/* 3 second send timeout (ms). */</span><span class="preprocessor"></span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#define CY_OTA_HTTP_TIMEOUT_RECEIVE         (3000)         </span><span class="comment">/* 3 second receive timeout. */</span><span class="preprocessor"></span></div>
<div class="line"></div>
<div class="line"><span class="comment">/**********************************************************************</span></div>
<div class="line"><span class="comment"> * Message Defines</span></div>
<div class="line"><span class="comment"> **********************************************************************/</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#define PUBLISHER_LISTEN_TOPIC              &quot;publish_notify&quot;</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#define COMPANY_TOPIC_PREPEND               &quot;OTAUpdate&quot;</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#define PUBLISHER_DIRECT_TOPIC               &quot;OTAImage&quot;</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#define CY_OTA_RESULT_SUCCESS               &quot;Success&quot;</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#define CY_OTA_RESULT_FAILURE               &quot;Failure&quot;</span></div>
<div class="line"></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#define CY_OTA_HTTP_JOB_FILE               &quot;/ota_update.json&quot;</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#define CY_OTA_HTTP_DATA_FILE              &quot;/ota-update.bin&quot;</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#define CY_OTA_SUBSCRIBE_UPDATES_AVAIL \</span></div>
<div class="line"><span class="preprocessor">&quot;{\</span></div>
<div class="line"><span class="preprocessor">\&quot;Message\&quot;:\&quot;Update Availability\&quot;, \</span></div>
<div class="line"><span class="preprocessor">\&quot;Manufacturer\&quot;: \&quot;Express Widgits Corporation\&quot;, \</span></div>
<div class="line"><span class="preprocessor">\&quot;ManufacturerID\&quot;: \&quot;EWCO\&quot;, \</span></div>
<div class="line"><span class="preprocessor">\&quot;ProductID\&quot;: \&quot;Easy Widgit\&quot;, \</span></div>
<div class="line"><span class="preprocessor">\&quot;SerialNumber\&quot;: \&quot;ABC213450001\&quot;, \</span></div>
<div class="line"><span class="preprocessor">\&quot;BoardName\&quot;: \&quot;CY8CPROTO_062_4343W\&quot;, \</span></div>
<div class="line"><span class="preprocessor">\&quot;Version\&quot;: \&quot;%d.%d.%d\&quot;, \</span></div>
<div class="line"><span class="preprocessor">\&quot;UniqueTopicName\&quot;: \&quot;%s\&quot;\</span></div>
<div class="line"><span class="preprocessor">}&quot;</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#define CY_OTA_DOWNLOAD_REQUEST \</span></div>
<div class="line"><span class="preprocessor">&quot;{\</span></div>
<div class="line"><span class="preprocessor">\&quot;Message\&quot;:\&quot;Request Update\&quot;, \</span></div>
<div class="line"><span class="preprocessor">\&quot;Manufacturer\&quot;: \&quot;Express Widgits Corporation\&quot;, \</span></div>
<div class="line"><span class="preprocessor">\&quot;ManufacturerID\&quot;: \&quot;EWCO\&quot;, \</span></div>
<div class="line"><span class="preprocessor">\&quot;ProductID\&quot;: \&quot;Easy Widgit\&quot;, \</span></div>
<div class="line"><span class="preprocessor">\&quot;SerialNumber\&quot;: \&quot;ABC213450001\&quot;, \</span></div>
<div class="line"><span class="preprocessor">\&quot;BoardName\&quot;: \&quot;CY8CPROTO_062_4343W\&quot;, \</span></div>
<div class="line"><span class="preprocessor">\&quot;Version\&quot;: \&quot;%d.%d.%d\&quot;, \</span></div>
<div class="line"><span class="preprocessor">\&quot;UniqueTopicName\&quot;: \&quot;%s\&quot; \</span></div>
<div class="line"><span class="preprocessor">}&quot;</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#define CY_OTA_DOWNLOAD_CHUNK_REQUEST \</span></div>
<div class="line"><span class="preprocessor">&quot;{\</span></div>
<div class="line"><span class="preprocessor">\&quot;Message\&quot;:\&quot;Request Data Chunk\&quot;, \</span></div>
<div class="line"><span class="preprocessor">\&quot;Manufacturer\&quot;: \&quot;Express Widgits Corporation\&quot;, \</span></div>
<div class="line"><span class="preprocessor">\&quot;ManufacturerID\&quot;: \&quot;EWCO\&quot;, \</span></div>
<div class="line"><span class="preprocessor">\&quot;ProductID\&quot;: \&quot;Easy Widgit\&quot;, \</span></div>
<div class="line"><span class="preprocessor">\&quot;SerialNumber\&quot;: \&quot;ABC213450001\&quot;, \</span></div>
<div class="line"><span class="preprocessor">\&quot;BoardName\&quot;: \&quot;CY8CPROTO_062_4343W\&quot;, \</span></div>
<div class="line"><span class="preprocessor">\&quot;Version\&quot;: \&quot;%d.%d.%d\&quot;, \</span></div>
<div class="line"><span class="preprocessor">\&quot;UniqueTopicName\&quot;: \&quot;%s\&quot;, \</span></div>
<div class="line"><span class="preprocessor">\&quot;Filename\&quot;: \&quot;%s\&quot;, \</span></div>
<div class="line"><span class="preprocessor">\&quot;Offset\&quot;: \&quot;%ld\&quot;, \</span></div>
<div class="line"><span class="preprocessor">\&quot;Size\&quot;: \&quot;%ld\&quot;\</span></div>
<div class="line"><span class="preprocessor">}&quot;</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#define CY_OTA_MQTT_RESULT_JSON \</span></div>
<div class="line"><span class="preprocessor">&quot;{\</span></div>
<div class="line"><span class="preprocessor">\&quot;Message\&quot;:\&quot;%s\&quot;, \</span></div>
<div class="line"><span class="preprocessor">\&quot;UniqueTopicName\&quot;: \&quot;%s\&quot;\</span></div>
<div class="line"><span class="preprocessor">}&quot;</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#define CY_OTA_HTTP_RESULT_JSON \</span></div>
<div class="line"><span class="preprocessor">&quot;{\</span></div>
<div class="line"><span class="preprocessor">\&quot;Message\&quot;:\&quot;%s\&quot;, \</span></div>
<div class="line"><span class="preprocessor">\&quot;File\&quot;:\&quot;%s\&quot; \</span></div>
<div class="line"><span class="preprocessor">}&quot;</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#ifndef CY_OTA_HTTP_GET_TEMPLATE</span></div>
<div class="line"><span class="preprocessor">#define CY_OTA_HTTP_GET_TEMPLATE \</span></div>
<div class="line"><span class="preprocessor">    &quot;GET %s HTTP/1.1\r\n&quot; \</span></div>
<div class="line"><span class="preprocessor">    &quot;Host: %s:%d \r\n&quot; \</span></div>
<div class="line"><span class="preprocessor">    &quot;\r\n&quot;</span></div>
<div class="line"><span class="preprocessor">#endif</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#ifndef CY_OTA_HTTP_GET_RANGE_TEMPLATE</span></div>
<div class="line"><span class="preprocessor">#define CY_OTA_HTTP_GET_RANGE_TEMPLATE \</span></div>
<div class="line"><span class="preprocessor">    &quot;GET %s HTTP/1.1\r\n&quot; \</span></div>
<div class="line"><span class="preprocessor">    &quot;Host: %s:%d \r\n&quot; \</span></div>
<div class="line"><span class="preprocessor">    &quot;Range: bytes=%ld-%ld \r\n&quot; \</span></div>
<div class="line"><span class="preprocessor">    &quot;\r\n&quot;</span></div>
<div class="line"><span class="preprocessor">#endif</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#ifndef CY_OTA_HTTP_POST_TEMPLATE</span></div>
<div class="line"><span class="preprocessor">#define CY_OTA_HTTP_POST_TEMPLATE \</span></div>
<div class="line"><span class="preprocessor">    &quot;POST %s HTTP/1.1\r\n&quot; \</span></div>
<div class="line"><span class="preprocessor">    &quot;Content-Length:%ld \r\n&quot; \</span></div>
<div class="line"><span class="preprocessor">    &quot;\r\n%s&quot;</span></div>
<div class="line"><span class="preprocessor">#endif</span></div>
<div class="line"></div>
<div class="line"><span class="comment">/**********************************************************************</span></div>
<div class="line"><span class="comment"> * MQTT Defines</span></div>
<div class="line"><span class="comment"> **********************************************************************/</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#define CY_OTA_MQTT_KEEP_ALIVE_SECONDS          (60)                </span><span class="comment">/* 60 second keepalive. */</span><span class="preprocessor"></span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#define CY_OTA_MQTT_MAX_TOPICS                  (2)</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#define CY_OTA_MQTT_TOPIC_PREFIX                &quot;cy_ota_device&quot;</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#define CY_OTA_MQTT_CLIENT_ID_PREFIX            &quot;cy_device&quot;</span></div>
<div class="line"></div>
</div><!-- fragment --><h1><a class="anchor" id="section_bootloader_support"></a>
Bootloader Support</h1>
<p><br />
 Starting from the 4.0 version of the ota-update library, the library is now fully separated from the MCUBootloader. This means that it can function independently and work with any bootloader, as long as the required OTA update handling storage APIs are implemented and registered with OTA agent by the user. <br />
 </p>
<p><b>1. MCUBootloader Support</b><br />
 MCUBootloader is a secure bootloader for 32-bits microcontrollers and users should build it outside of the user OTA application. It is programmed separately to the device before flashing the user OTA application and is not updated for the life of the device. To support MCUBootloader based OTA using ota-update library, User can implement his own storage operation callbacks to handle upgrade image or can make use of <em>ota-bootloader-abstraction</em> library.<br />
 ota-bootloader-abstraction library has below support.<br />
</p><ol type="1">
<li>Template flashmaps for PSoC6, 20829, 89829 and XMC7000 platforms.<br />
</li>
<li>Template linker files for GCC_ARM, ARM, and IAR toolchains.<br />
</li>
<li>Storage operation callback APIs to handle MCUBootloader based upgrade image.<br />
</li>
<li>Prebuild and Postbuild scripts for generating and signing MCUBootloader based BOOT and UPGRADE image of an OTA Application.<br />
 </li>
</ol>
<p><b>2. In-built Bootloader Support on CYW955913EVK-01 kit</b><br />
 Platforms like CYW955913EVK-01 have in-built bootloader. To support OTA using ota-update library on CYW955913EVK-01 kit, user can implement his own storage operation callbacks to handle upgrade image or can make use of ota-bootloader-abstraction library.<br />
 ota-bootloader-abstraction library has below support for in-built bootloader based platforms like CYW955913EVK-01.<br />
</p><ol type="1">
<li>Storage operation callback APIs to handle CYW955913EVK-01 in-built Bootloader based upgrade image.<br />
 </li>
</ol>
<p>User needs to register storage operation callbacks available in ota-bootloader-abstraction during OTA agent start.<br />
 For more information, see the various README.md files in the ota-bootloader-abstraction/&lt;version&gt;/ directory.</p>
<h1><a class="anchor" id="section_snippets"></a>
Code Snippets</h1>
<p><b>MQTT and HTTP initialization:</b><br />
 The following code snippet demonstrates an example for initializing the <a class="el" href="structcy__ota__network__params__t.html" title="OTA Connection parameters structure. ">cy_ota_network_params_t</a> and ota_agent_params structures required to start the OTA Agent.<br />
 </p><div class="fragment"><div class="line"><span class="comment">/* Macro to enable/disable TLS. */</span></div>
<div class="line"><span class="preprocessor">#define ENABLE_TLS          (false)</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#define HTTP_SERVER         &quot;my.httpserver.com&quot;</span></div>
<div class="line"><span class="preprocessor">#define HTTP_SERVER_PORT    (80)                </span><span class="comment">/* 443 for TLS. */</span><span class="preprocessor"></span></div>
<div class="line"></div>
<div class="line"><span class="comment">/* Number of MQTT topic filters. */</span></div>
<div class="line"><span class="preprocessor">#define MQTT_TOPIC_FILTER_NUM   (1)</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#define MQTT_BROKER         &quot;mqtt.broker.com&quot;</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#define MQTT_BROKER_PORT    (1883)              </span><span class="comment">/* (8883) for TLS support. */</span><span class="preprocessor"></span></div>
<div class="line"><span class="comment">/* MQTT topics. */</span></div>
<div class="line"><span class="keyword">const</span> <span class="keywordtype">char</span> * mqtt_topics[ MQTT_TOPIC_FILTER_NUM ] =</div>
<div class="line">{</div>
<div class="line">        <span class="stringliteral">&quot;mtb/test/ota/image&quot;</span></div>
<div class="line">};</div>
<div class="line"></div>
<div class="line"><span class="comment">/* Root CA Certificate.</span></div>
<div class="line"><span class="comment">   Must include the PEM header and footer:</span></div>
<div class="line"><span class="comment"></span></div>
<div class="line"><span class="comment">        &quot;-----BEGIN CERTIFICATE-----\n&quot; \</span></div>
<div class="line"><span class="comment">        &quot;.........base64 data.......\n&quot; \</span></div>
<div class="line"><span class="comment">        &quot;-----END CERTIFICATE-------\n&quot;</span></div>
<div class="line"><span class="comment">*/</span></div>
<div class="line"><span class="preprocessor">#define ROOT_CA_CERTIFICATE     &quot;&quot;</span></div>
<div class="line"></div>
<div class="line"><span class="comment">/* Client Certificate.</span></div>
<div class="line"><span class="comment">   Must include the PEM header and footer:</span></div>
<div class="line"><span class="comment"></span></div>
<div class="line"><span class="comment">        &quot;-----BEGIN CERTIFICATE-----\n&quot; \</span></div>
<div class="line"><span class="comment">        &quot;.........base64 data.......\n&quot; \</span></div>
<div class="line"><span class="comment">        &quot;-----END CERTIFICATE-------\n&quot;</span></div>
<div class="line"><span class="comment">*/</span></div>
<div class="line"><span class="preprocessor">#define CLIENT_CERTIFICATE      &quot;&quot;</span></div>
<div class="line"></div>
<div class="line"></div>
<div class="line"><span class="comment">/* Private Key.</span></div>
<div class="line"><span class="comment">   Must include the PEM header and footer:</span></div>
<div class="line"><span class="comment"></span></div>
<div class="line"><span class="comment">        &quot;-----BEGIN RSA PRIVATE KEY-----\n&quot; \</span></div>
<div class="line"><span class="comment">        &quot;...........base64 data.........\n&quot; \</span></div>
<div class="line"><span class="comment">        &quot;-----END RSA PRIVATE KEY-------\n&quot;</span></div>
<div class="line"><span class="comment">*/</span></div>
<div class="line"><span class="preprocessor">#define CLIENT_KEY              &quot;&quot;</span></div>
<div class="line"></div>
<div class="line"><span class="comment">/* OTA context. */</span></div>
<div class="line"><span class="keyword">static</span> <a class="code" href="group__group__ota__typedefs.html#ga50b8b1ec191a1d5f9a8fcd3472811694">cy_ota_context_ptr</a> ota_context;</div>
<div class="line"></div>
<div class="line"><span class="comment">/* OTA callback function define. */</span></div>
<div class="line"><a class="code" href="group__group__ota__typedefs.html#ga83280df2ca46db528cc12901d949fd63">cy_ota_callback_results_t</a> ota_callback(<a class="code" href="structcy__ota__cb__struct__t.html">cy_ota_cb_struct_t</a> *cb_data);</div>
<div class="line"></div>
<div class="line"> <span class="comment">/* MQTT Credentials for OTA. */</span></div>
<div class="line">cy_awsport_ssl_credentials_t    mqtt_credentials;</div>
<div class="line"></div>
<div class="line"><span class="comment">/* HTTP Credentials for OTA. */</span></div>
<div class="line">cy_awsport_ssl_credentials_t    http_credentials;</div>
<div class="line"></div>
<div class="line"><span class="comment">/* network parameters for OTA. */</span></div>
<div class="line"><a class="code" href="structcy__ota__network__params__t.html">cy_ota_network_params_t</a>     network_params = { <a class="code" href="group__group__ota__typedefs.html#ggabc03646427dec963bef37cb5dbb550faa141f64e2edc3273de1e0374dc8e681ca">CY_OTA_CONNECTION_UNKNOWN</a> };</div>
<div class="line"></div>
<div class="line"><span class="comment">/* Parameters for the OTA Agent. */</span></div>
<div class="line"><a class="code" href="structcy__ota__agent__params__t.html">cy_ota_agent_params_t</a> ota_agent_params =</div>
<div class="line">{</div>
<div class="line">    .<a class="code" href="structcy__ota__agent__params__t.html#a2f3b68d908f64e031aca84d871554850">cb_func</a> = ota_callback,                <span class="comment">/* Set the value to NULL if the callback function is not used. */</span></div>
<div class="line">    .cb_arg = &amp;ota_context,                 <span class="comment">/* Set the value to NULL if the callback function is not used. */</span></div>
<div class="line">    .reboot_upon_completion = 1             <span class="comment">/* Reboot after completing OTA with success. */</span></div>
<div class="line">};</div>
<div class="line"></div>
<div class="line"><a class="code" href="structcy__ota__storage__interface__t.html">cy_ota_storage_interface_t</a> ota_interfaces =</div>
<div class="line">{</div>
<div class="line">   .<a class="code" href="structcy__ota__storage__interface__t.html#a82221089aeadb8fafc41b2b296bf5b8f">ota_file_open</a>            = cy_ota_storage_open,</div>
<div class="line">   .ota_file_read            = cy_ota_storage_read,</div>
<div class="line">   .ota_file_write           = cy_ota_storage_write,</div>
<div class="line">   .ota_file_close           = cy_ota_storage_close,</div>
<div class="line">   .ota_file_verify          = cy_ota_storage_verify,</div>
<div class="line">   .ota_file_validate        = cy_ota_storage_validated,</div>
<div class="line">   .ota_file_get_app_info    = cy_ota_storage_get_app_info</div>
<div class="line">};</div>
<div class="line"></div>
</div><!-- fragment --><p> <br />
 <b>OTA flash operations callback:</b><br />
 This example functions demonstrates the usage of the OTA storage operation callbacks to handle upgrade image(read/write/erase). Implementation of storage operation callbacks is mandatory because OTA Agent depends on these callbacks for handling upgrade images.<br />
 <br />
 Note: You must initialize Flash before registering these callbacks.<br />
 </p><div class="fragment"><div class="line"><span class="comment">/*******************************************************************************</span></div>
<div class="line"><span class="comment"> * Function Name: cy_ota_storage_open()</span></div>
<div class="line"><span class="comment"> *******************************************************************************</span></div>
<div class="line"><span class="comment"> * Summary:</span></div>
<div class="line"><span class="comment"> *  Got a callback from OTA for opening and initializing storage area for download.</span></div>
<div class="line"><span class="comment"> *  Typically, this erases upgrade slot.</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> *  @param[in]   storage_ptr     Pointer to the OTA Agent storage context @ref cy_ota_storage_context_t</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> *  @return  CY_RSLT_SUCCESS</span></div>
<div class="line"><span class="comment"> *           CY_RSLT_OTA_ERROR_OPEN_STORAGE</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> *******************************************************************************/</span></div>
<div class="line">cy_rslt_t cy_ota_storage_open(<a class="code" href="structcy__ota__storage__context__t.html">cy_ota_storage_context_t</a> *storage_ptr)</div>
<div class="line">{</div>
<div class="line">    cy_rslt_t result = CY_RSLT_SUCCESS;</div>
<div class="line"></div>
<div class="line">    <span class="keywordflow">if</span>(storage_ptr == NULL)</div>
<div class="line">    {</div>
<div class="line">        <span class="comment">/* Parameter Error */</span></div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    <span class="comment">/* Add logic to open and erase storage area for writing upgrade */</span></div>
<div class="line">    <span class="keywordflow">return</span> result;</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="comment">/*******************************************************************************</span></div>
<div class="line"><span class="comment"> * Function Name: cy_ota_storage_read()</span></div>
<div class="line"><span class="comment"> *******************************************************************************</span></div>
<div class="line"><span class="comment"> * Summary:</span></div>
<div class="line"><span class="comment"> *  Got a callback from OTA to read from storage area.</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> * @param[in]       storage_ptr - Pointer to the OTA Agent storage context @ref cy_ota_storage_context_t</span></div>
<div class="line"><span class="comment"> * @param[in][out]  chunk_info  - Pointer to chunk information, buffer pointer used for the read</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> *  @return  CY_RSLT_SUCCESS</span></div>
<div class="line"><span class="comment"> *           CY_RSLT_OTA_ERROR_READ_STORAGE</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> *******************************************************************************/</span></div>
<div class="line">cy_rslt_t cy_ota_storage_read(<a class="code" href="structcy__ota__storage__context__t.html">cy_ota_storage_context_t</a> *storage_ptr, <a class="code" href="structcy__ota__storage__write__info__t.html">cy_ota_storage_read_info_t</a> *chunk_info)</div>
<div class="line">{</div>
<div class="line">    cy_rslt_t result = CY_RSLT_SUCCESS;</div>
<div class="line"></div>
<div class="line">    <span class="keywordflow">if</span>(storage_ptr == NULL)</div>
<div class="line">    {</div>
<div class="line">        <span class="comment">/* Parameter Error */</span></div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    <span class="comment">/* Add logic to read from storage area */</span></div>
<div class="line">    <span class="keywordflow">return</span> result;</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="comment">/*******************************************************************************</span></div>
<div class="line"><span class="comment"> * Function Name: cy_ota_storage_write()</span></div>
<div class="line"><span class="comment"> *******************************************************************************</span></div>
<div class="line"><span class="comment"> * Summary:</span></div>
<div class="line"><span class="comment"> *  Got a callback from OTA for writing to storage area.</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> * @param[in]   storage_ptr     Pointer to the OTA Agent storage context @ref cy_ota_storage_context_t</span></div>
<div class="line"><span class="comment"> * @param[in]   chunk_info      Pointer to chunk information, buffer pointer used for the write</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> *  @return  CY_RSLT_SUCCESS</span></div>
<div class="line"><span class="comment"> *           CY_RSLT_OTA_ERROR_WRITE_STORAGE</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> *******************************************************************************/</span></div>
<div class="line">cy_rslt_t cy_ota_storage_write(<a class="code" href="structcy__ota__storage__context__t.html">cy_ota_storage_context_t</a> *storage_ptr, <a class="code" href="structcy__ota__storage__write__info__t.html">cy_ota_storage_write_info_t</a> * <span class="keyword">const</span> chunk_info)</div>
<div class="line">{</div>
<div class="line">    cy_rslt_t result = CY_RSLT_SUCCESS;</div>
<div class="line"></div>
<div class="line">    <span class="keywordflow">if</span>(storage_ptr == NULL)</div>
<div class="line">    {</div>
<div class="line">        <span class="comment">/* Parameter Error */</span></div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    <span class="comment">/* Add logic to write upgrade image chunks/information to storage area */</span></div>
<div class="line">    <span class="keywordflow">return</span> result;</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="comment">/*******************************************************************************</span></div>
<div class="line"><span class="comment"> * Function Name: cy_ota_storage_close()</span></div>
<div class="line"><span class="comment"> *******************************************************************************</span></div>
<div class="line"><span class="comment"> * Summary:</span></div>
<div class="line"><span class="comment"> *  Got a callback from OTA for closing storage area.</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> *  @param[in]   storage_ptr     Pointer to the OTA Agent storage context @ref cy_ota_storage_context_t</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> *  @return  CY_RSLT_SUCCESS</span></div>
<div class="line"><span class="comment"> *           CY_RSLT_OTA_ERROR_CLOSE_STORAGE</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> *******************************************************************************/</span></div>
<div class="line">cy_rslt_t cy_ota_storage_close(<a class="code" href="structcy__ota__storage__context__t.html">cy_ota_storage_context_t</a> *storage_ptr)</div>
<div class="line">{</div>
<div class="line">    cy_rslt_t result = CY_RSLT_SUCCESS;</div>
<div class="line"></div>
<div class="line">    <span class="keywordflow">if</span>(storage_ptr == NULL)</div>
<div class="line">    {</div>
<div class="line">        <span class="comment">/* Parameter Error */</span></div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    <span class="comment">/* Add logic to close storage area */</span></div>
<div class="line">    <span class="keywordflow">return</span> result;</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="comment">/*******************************************************************************</span></div>
<div class="line"><span class="comment"> * Function Name: cy_ota_storage_verify()</span></div>
<div class="line"><span class="comment"> *******************************************************************************</span></div>
<div class="line"><span class="comment"> * Summary:</span></div>
<div class="line"><span class="comment"> *  Got a callback from OTA to verify the downloaded upgrade image.</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> *  @param[in]   storage_ptr     Pointer to the OTA Agent storage context @ref cy_ota_storage_context_t</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> *  @return  CY_RSLT_SUCCESS</span></div>
<div class="line"><span class="comment"> *           CY_RSLT_OTA_ERROR_GENERAL</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> *******************************************************************************/</span></div>
<div class="line">cy_rslt_t cy_ota_storage_verify(<a class="code" href="structcy__ota__storage__context__t.html">cy_ota_storage_context_t</a> *storage_ptr)</div>
<div class="line">{</div>
<div class="line">    cy_rslt_t result = CY_RSLT_SUCCESS;</div>
<div class="line"></div>
<div class="line">    <span class="keywordflow">if</span>(storage_ptr == NULL)</div>
<div class="line">    {</div>
<div class="line">        <span class="comment">/* Parameter Error */</span></div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    <span class="comment">/* Add logic to verify downloaded image and Activate if verification is successful. */</span></div>
<div class="line">    <span class="keywordflow">return</span> result;</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="comment">/*******************************************************************************</span></div>
<div class="line"><span class="comment"> * Function Name: cy_ota_storage_validated()</span></div>
<div class="line"><span class="comment"> *******************************************************************************</span></div>
<div class="line"><span class="comment"> * Summary:</span></div>
<div class="line"><span class="comment"> *  This API needs to be called from application to make active image as permanent Image.</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> *  @return  CY_RSLT_SUCCESS</span></div>
<div class="line"><span class="comment"> *           CY_RSLT_OTA_ERROR_GENERAL</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> *******************************************************************************/</span></div>
<div class="line">cy_rslt_t cy_ota_storage_validated(<span class="keywordtype">void</span>)</div>
<div class="line">{</div>
<div class="line">    cy_rslt_t result = CY_RSLT_SUCCESS;</div>
<div class="line"></div>
<div class="line">    <span class="keywordflow">if</span>(storage_ptr == NULL)</div>
<div class="line">    {</div>
<div class="line">        <span class="comment">/* Parameter Error */</span></div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    <span class="comment">/* Add logic to to make active image as permanent image. */</span></div>
<div class="line">    <span class="keywordflow">return</span> result;</div>
<div class="line">}</div>
<div class="line"><span class="comment">/*******************************************************************************</span></div>
<div class="line"><span class="comment"> * Function Name: cy_ota_storage_verify()</span></div>
<div class="line"><span class="comment"> *******************************************************************************</span></div>
<div class="line"><span class="comment"> * Summary:</span></div>
<div class="line"><span class="comment"> *  Got a callback from OTA to read BOOT/UPGRADE image information.</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> *  @param[in]        file_des     Pointer to the storage context.</span></div>
<div class="line"><span class="comment"> *  @param[in][out]   app_info     Pointer to store application image information.</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> *  @return  CY_RSLT_SUCCESS</span></div>
<div class="line"><span class="comment"> *           CY_RSLT_OTA_ERROR_GENERAL</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> *******************************************************************************/</span></div>
<div class="line">cy_rslt_t cy_ota_storage_get_app_info(<span class="keywordtype">void</span>* file_des, <a class="code" href="structcy__ota__app__info__t.html">cy_ota_app_info_t</a> *app_info)</div>
<div class="line">{</div>
<div class="line">    cy_rslt_t result = CY_RSLT_SUCCESS;</div>
<div class="line"></div>
<div class="line">    <span class="keywordflow">if</span>(storage_ptr == NULL)</div>
<div class="line">    {</div>
<div class="line">        <span class="comment">/* Parameter Error */</span></div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    <span class="comment">/* Add logic to read BOOT/UPGRADE image information such as version and ID. */</span></div>
<div class="line">    <span class="keywordflow">return</span> result;</div>
<div class="line">}</div>
<div class="line"></div>
</div><!-- fragment --><p> <br />
 <b>OTA Application callback:</b><br />
 This example function demonstrates the usage of the OTA callback function to print the status of every OTA event. The callback feature is optional but be aware that the OTA library will not print the status of the OTA Agent on its own.<br />
 <br />
 Note: You must initialize retarget-io to use the debug UART port.<br />
 </p><div class="fragment"><div class="line"><span class="comment">/*******************************************************************************</span></div>
<div class="line"><span class="comment"> * Function Name: ota_callback()</span></div>
<div class="line"><span class="comment"> *******************************************************************************</span></div>
<div class="line"><span class="comment"> * Summary:</span></div>
<div class="line"><span class="comment"> *  Got a callback from OTA.</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> *  @param[in][out] cb_data - Structure holding the information for the application.</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> * Return:</span></div>
<div class="line"><span class="comment"> *  CY_OTA_CB_RSLT_OTA_CONTINUE - OTA Agent to continue with the function, using the modified data from the application.</span></div>
<div class="line"><span class="comment"> *  CY_OTA_CB_RSLT_OTA_STOP     - OTA Agent to end the current update session (do not quit).</span></div>
<div class="line"><span class="comment"> *  CY_OTA_CB_RSLT_APP_SUCCESS  - Application completed task; OTA Agent use success.</span></div>
<div class="line"><span class="comment"> *  CY_OTA_CB_RSLT_APP_FAILED   - Application completed task; OTA Agent use failure.</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> *******************************************************************************/</span></div>
<div class="line"><span class="keyword">static</span> <a class="code" href="group__group__ota__typedefs.html#ga83280df2ca46db528cc12901d949fd63">cy_ota_callback_results_t</a> ota_callback(<a class="code" href="structcy__ota__cb__struct__t.html">cy_ota_cb_struct_t</a> *cb_data)</div>
<div class="line">{</div>
<div class="line">    <a class="code" href="group__group__ota__typedefs.html#ga83280df2ca46db528cc12901d949fd63">cy_ota_callback_results_t</a>   cb_result = <a class="code" href="group__group__ota__typedefs.html#gga83280df2ca46db528cc12901d949fd63a9b6b16fa9fcb36760749ced06ea21513">CY_OTA_CB_RSLT_OTA_CONTINUE</a>;    <span class="comment">/* OTA Agent to continue as normal. */</span></div>
<div class="line">    <span class="keyword">const</span> <span class="keywordtype">char</span>                  *state_string;</div>
<div class="line">    <span class="keyword">const</span> <span class="keywordtype">char</span>                  *error_string;</div>
<div class="line">    <a class="code" href="group__group__ota__typedefs.html#ga50b8b1ec191a1d5f9a8fcd3472811694">cy_ota_context_ptr</a>          ctx = NULL;</div>
<div class="line"></div>
<div class="line">    <span class="keywordflow">if</span> (cb_data == NULL)</div>
<div class="line">    {</div>
<div class="line">        <span class="keywordflow">return</span> <a class="code" href="group__group__ota__typedefs.html#gga83280df2ca46db528cc12901d949fd63ab33edc4fae91e1c99f93933fc3a06bd5">CY_OTA_CB_RSLT_OTA_STOP</a>;</div>
<div class="line">    }</div>
<div class="line">    ctx = *((<a class="code" href="group__group__ota__typedefs.html#ga50b8b1ec191a1d5f9a8fcd3472811694">cy_ota_context_ptr</a> *)cb_data-&gt;<a class="code" href="structcy__ota__cb__struct__t.html#afa47e10a9fe4712558dded6625a3c484">cb_arg</a>);</div>
<div class="line">    <span class="keywordflow">if</span> (ctx == NULL)</div>
<div class="line">    {</div>
<div class="line">        <span class="keywordflow">return</span> <a class="code" href="group__group__ota__typedefs.html#gga83280df2ca46db528cc12901d949fd63ab33edc4fae91e1c99f93933fc3a06bd5">CY_OTA_CB_RSLT_OTA_STOP</a>;</div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    state_string = <a class="code" href="group__group__ota__functions.html#gafb857a3ac5167224f6f3af877a33ac32">cy_ota_get_state_string</a>(cb_data-&gt;state);</div>
<div class="line">    error_string = <a class="code" href="group__group__ota__functions.html#gaffc74d5bb4dd74ede1031222b9efcd9b">cy_ota_get_error_string</a>(<a class="code" href="group__group__ota__functions.html#ga7149423078fe0b9d401bddc1d0e808d1">cy_ota_get_last_error</a>());</div>
<div class="line">    reason_string = <a class="code" href="group__group__ota__functions.html#ga1f3276614aeca5a8d7c176f171024d66">cy_ota_get_callback_reason_string</a>(cb_data-&gt;<a class="code" href="structcy__ota__cb__struct__t.html#ae28ffd179023e8902cbd3e88785540ad">reason</a>);</div>
<div class="line"></div>
<div class="line">    <span class="keywordflow">switch</span> (cb_data-&gt;<a class="code" href="structcy__ota__cb__struct__t.html#ae28ffd179023e8902cbd3e88785540ad">reason</a>)</div>
<div class="line">    {</div>
<div class="line"></div>
<div class="line">    <span class="keywordflow">case</span> <a class="code" href="group__group__ota__typedefs.html#ggad852fa8cdb0431df709ccf12bb37cd60a6f1fe12d222a4fcb6aef59d045cc40af">CY_OTA_LAST_REASON</a>:                <span class="comment">/* To keep linker happy. */</span></div>
<div class="line">        <span class="keywordflow">break</span>;</div>
<div class="line"></div>
<div class="line">    <span class="keywordflow">case</span> <a class="code" href="group__group__ota__typedefs.html#ggad852fa8cdb0431df709ccf12bb37cd60a98710a7a1d2678890cda211ab2c89689">CY_OTA_REASON_SUCCESS</a>:</div>
<div class="line">        printf(<span class="stringliteral">&quot;APP CB OTA SUCCESS state:%d %s last_error:%s\n&quot;</span>, cb_data-&gt;state, state_string, error_string);</div>
<div class="line">        <span class="keywordflow">break</span>;</div>
<div class="line">    <span class="keywordflow">case</span> <a class="code" href="group__group__ota__typedefs.html#ggad852fa8cdb0431df709ccf12bb37cd60ad2bf02c326de5c6c95551123155e14bd">CY_OTA_REASON_FAILURE</a>:</div>
<div class="line">        printf(<span class="stringliteral">&quot;APP CB OTA FAILURE state:%d %s last_error:%s\n&quot;</span>, cb_data-&gt;state, state_string, error_string);</div>
<div class="line">        <span class="keywordflow">break</span>;</div>
<div class="line">    <span class="keywordflow">case</span> <a class="code" href="group__group__ota__typedefs.html#ggad852fa8cdb0431df709ccf12bb37cd60a7d2f0693d44fd480e23c7de3c2f68d10">CY_OTA_REASON_STATE_CHANGE</a>:</div>
<div class="line"></div>
<div class="line">        <span class="keywordflow">switch</span> (cb_data-&gt;state)</div>
<div class="line">        {</div>
<div class="line">            <span class="comment">/* Informational. */</span></div>
<div class="line">        <span class="keywordflow">case</span> <a class="code" href="group__group__ota__typedefs.html#ggab1539ebc23219f1ca64318286e9d375fae28d1cd8c5a985bb343a0828c234fdd0">CY_OTA_STATE_NOT_INITIALIZED</a>:</div>
<div class="line">        <span class="keywordflow">case</span> <a class="code" href="group__group__ota__typedefs.html#ggab1539ebc23219f1ca64318286e9d375fab4dff9b84c5eafa48e4d3eaff2302ba5">CY_OTA_STATE_EXITING</a>:</div>
<div class="line">        <span class="keywordflow">case</span> <a class="code" href="group__group__ota__typedefs.html#ggab1539ebc23219f1ca64318286e9d375fa4980922c9b3cf3c4e1269324d4563d78">CY_OTA_STATE_INITIALIZING</a>:</div>
<div class="line">        <span class="keywordflow">case</span> <a class="code" href="group__group__ota__typedefs.html#ggab1539ebc23219f1ca64318286e9d375faa1a4033af52d14af2873066da8f89098">CY_OTA_STATE_AGENT_STARTED</a>:</div>
<div class="line">        <span class="keywordflow">case</span> <a class="code" href="group__group__ota__typedefs.html#ggab1539ebc23219f1ca64318286e9d375faf2dbf8dcfb8d9329549fa539970d9e54">CY_OTA_STATE_AGENT_WAITING</a>:</div>
<div class="line">            printf(<span class="stringliteral">&quot;APP CB OTA STATE CHANGE state:%d %s last_error:%s\n&quot;</span>, cb_data-&gt;state, state_string, error_string);</div>
<div class="line">            <span class="keywordflow">break</span>;</div>
<div class="line"></div>
<div class="line">        <span class="keywordflow">case</span> <a class="code" href="group__group__ota__typedefs.html#ggab1539ebc23219f1ca64318286e9d375fad8e99bc84c807fa17f23b119900d4c1a">CY_OTA_STATE_START_UPDATE</a>:</div>
<div class="line">            printf(<span class="stringliteral">&quot;APP CB OTA STATE CHANGE CY_OTA_STATE_START_UPDATE\n&quot;</span>);</div>
<div class="line">            <span class="keywordflow">break</span>;</div>
<div class="line"></div>
<div class="line">        <span class="keywordflow">case</span> <a class="code" href="group__group__ota__typedefs.html#ggab1539ebc23219f1ca64318286e9d375fa4b481536d237fc9672cff019e5a24392">CY_OTA_STATE_STORAGE_OPEN</a>:</div>
<div class="line">            printf(<span class="stringliteral">&quot;APP CB OTA STORAGE OPEN\n&quot;</span>);</div>
<div class="line">            <span class="keywordflow">break</span>;</div>
<div class="line">        <span class="keywordflow">case</span> <a class="code" href="group__group__ota__typedefs.html#ggab1539ebc23219f1ca64318286e9d375faf8b6a7adfb042c0b4379db005eb03fcb">CY_OTA_STATE_STORAGE_WRITE</a>:</div>
<div class="line">            printf(<span class="stringliteral">&quot;APP CB OTA STORAGE WRITE %ld%% (%ld of %ld)\n&quot;</span>, cb_data-&gt;<a class="code" href="structcy__ota__cb__struct__t.html#a1d802b856542ce6b7e59e2559026307d">percentage</a>, cb_data-&gt;<a class="code" href="structcy__ota__cb__struct__t.html#ab43108a89dadf59dcad71d4b4c75411d">bytes_written</a>, cb_data-&gt;<a class="code" href="structcy__ota__cb__struct__t.html#ad29859423d69a8e0815a833a70284bdb">total_size</a>);</div>
<div class="line">            <span class="keywordflow">break</span>;</div>
<div class="line">        <span class="keywordflow">case</span> <a class="code" href="group__group__ota__typedefs.html#ggab1539ebc23219f1ca64318286e9d375fa8177ec48fc37847b69d33f90a3b4333b">CY_OTA_STATE_STORAGE_CLOSE</a>:</div>
<div class="line">            printf(<span class="stringliteral">&quot;APP CB OTA STORAGE CLOSE\n&quot;</span>);</div>
<div class="line">            <span class="keywordflow">break</span>;</div>
<div class="line"></div>
<div class="line">        <span class="keywordflow">case</span> <a class="code" href="group__group__ota__typedefs.html#ggab1539ebc23219f1ca64318286e9d375fafdf0e2e6deb544bc5bdc5dec53b0609f">CY_OTA_STATE_JOB_CONNECT</a>:</div>
<div class="line">            printf(<span class="stringliteral">&quot;APP CB OTA CONNECT FOR JOB using &quot;</span>);</div>
<div class="line">            <span class="comment">/* NOTE:</span></div>
<div class="line"><span class="comment">             *  MQTT - json_doc holds the MQTT JSON request document.</span></div>
<div class="line"><span class="comment">             *  HTTP - json_doc holds the HTTP &quot;GET&quot; request.</span></div>
<div class="line"><span class="comment">             */</span></div>
<div class="line">            <span class="keywordflow">if</span> (cb_data-&gt;<a class="code" href="structcy__ota__cb__struct__t.html#aabc069ca938caaeda44300df68d31903">connection_type</a> == <a class="code" href="group__group__ota__typedefs.html#ggabc03646427dec963bef37cb5dbb550faa7955097dd62d0b81cb447d287d12bd98">CY_OTA_CONNECTION_MQTT</a>)</div>
<div class="line">            {</div>
<div class="line">                printf(<span class="stringliteral">&quot;MQTT: server:%s port: %d\n&quot;</span>, cb_data-&gt;broker_server.pHostName, cb_data-&gt;broker_server.port);</div>
<div class="line">            }</div>
<div class="line">            <span class="keywordflow">else</span></div>
<div class="line">            {</div>
<div class="line">                printf(<span class="stringliteral">&quot;HTTP: server:%s port: %d\n&quot;</span>, cb_data-&gt;broker_server.pHostName, cb_data-&gt;broker_server.port);</div>
<div class="line">            }</div>
<div class="line">            <span class="keywordflow">break</span>;</div>
<div class="line">        <span class="keywordflow">case</span> <a class="code" href="group__group__ota__typedefs.html#ggab1539ebc23219f1ca64318286e9d375fa53922a893f09e01688b27c5d225e27a5">CY_OTA_STATE_JOB_DOWNLOAD</a>:</div>
<div class="line">            printf(<span class="stringliteral">&quot;APP CB OTA JOB DOWNLOAD using &quot;</span>);</div>
<div class="line">            <span class="comment">/* NOTE:</span></div>
<div class="line"><span class="comment">             *  MQTT - json_doc holds the MQTT JSON request document.</span></div>
<div class="line"><span class="comment">             *  HTTP - json_doc holds the HTTP &quot;GET&quot; request.</span></div>
<div class="line"><span class="comment">             */</span></div>
<div class="line">            <span class="keywordflow">if</span> (cb_data-&gt;<a class="code" href="structcy__ota__cb__struct__t.html#aabc069ca938caaeda44300df68d31903">connection_type</a> == <a class="code" href="group__group__ota__typedefs.html#ggabc03646427dec963bef37cb5dbb550faa7955097dd62d0b81cb447d287d12bd98">CY_OTA_CONNECTION_MQTT</a>)</div>
<div class="line">            {</div>
<div class="line">                printf(<span class="stringliteral">&quot;MQTT: &#39;%s&#39;&quot;</span>, cb_data-&gt;json_doc);</div>
<div class="line">                printf(<span class="stringliteral">&quot;                 topic: &#39;%s&#39; \n&quot;</span>, cb_data-&gt;unique_topic);</div>
<div class="line">            }</div>
<div class="line">            <span class="keywordflow">else</span></div>
<div class="line">            {</div>
<div class="line">                printf(<span class="stringliteral">&quot;HTTP: &#39;%s&#39;&quot;</span>, cb_data-&gt;file);</div>
<div class="line">            }</div>
<div class="line">            <span class="keywordflow">break</span>;</div>
<div class="line">        <span class="keywordflow">case</span> <a class="code" href="group__group__ota__typedefs.html#ggab1539ebc23219f1ca64318286e9d375fa5f9288e902dc5473fd3e681eb2c43012">CY_OTA_STATE_JOB_DISCONNECT</a>:</div>
<div class="line">            printf(<span class="stringliteral">&quot;APP CB OTA JOB DISCONNECT\n&quot;</span>);</div>
<div class="line">            <span class="keywordflow">break</span>;</div>
<div class="line"></div>
<div class="line">        <span class="keywordflow">case</span> <a class="code" href="group__group__ota__typedefs.html#ggab1539ebc23219f1ca64318286e9d375fa1a5597cb918e3a60890c003ca2b35926">CY_OTA_STATE_JOB_PARSE</a>:</div>
<div class="line">            printf(<span class="stringliteral">&quot;APP CB OTA PARSE JOB: &#39;%.*s&#39; \n&quot;</span>, strlen(cb_data-&gt;json_doc), cb_data-&gt;json_doc);</div>
<div class="line">            <span class="keywordflow">break</span>;</div>
<div class="line">        <span class="keywordflow">case</span> <a class="code" href="group__group__ota__typedefs.html#ggab1539ebc23219f1ca64318286e9d375fa7d1ad039e8599533b1bd50c4068a41da">CY_OTA_STATE_JOB_REDIRECT</a>:</div>
<div class="line">            printf(<span class="stringliteral">&quot;APP CB OTA JOB REDIRECT\n&quot;</span>);</div>
<div class="line">            <span class="keywordflow">break</span>;</div>
<div class="line"></div>
<div class="line">        <span class="keywordflow">case</span> <a class="code" href="group__group__ota__typedefs.html#ggab1539ebc23219f1ca64318286e9d375fab8c745b4b9dd7c582c1cc6735ad1401e">CY_OTA_STATE_DATA_CONNECT</a>:</div>
<div class="line">            printf(<span class="stringliteral">&quot;APP CB OTA CONNECT FOR DATA using &quot;</span>);</div>
<div class="line">            <span class="keywordflow">if</span> (cb_data-&gt;<a class="code" href="structcy__ota__cb__struct__t.html#aabc069ca938caaeda44300df68d31903">connection_type</a> == <a class="code" href="group__group__ota__typedefs.html#ggabc03646427dec963bef37cb5dbb550faa7955097dd62d0b81cb447d287d12bd98">CY_OTA_CONNECTION_MQTT</a>)</div>
<div class="line">            {</div>
<div class="line">                printf(<span class="stringliteral">&quot;MQTT: %s:%d \n&quot;</span>, cb_data-&gt;broker_server.pHostName, cb_data-&gt;broker_server.port);</div>
<div class="line">            }</div>
<div class="line">            <span class="keywordflow">else</span></div>
<div class="line">            {</div>
<div class="line">                printf(<span class="stringliteral">&quot;HTTP: %s:%d \n&quot;</span>, cb_data-&gt;broker_server.pHostName, cb_data-&gt;broker_server.port);</div>
<div class="line">            }</div>
<div class="line">            <span class="keywordflow">break</span>;</div>
<div class="line">        <span class="keywordflow">case</span> <a class="code" href="group__group__ota__typedefs.html#ggab1539ebc23219f1ca64318286e9d375fa2821523d7c323f672da8b19b45d5d935">CY_OTA_STATE_DATA_DOWNLOAD</a>:</div>
<div class="line">            printf(<span class="stringliteral">&quot;APP CB OTA DATA DOWNLOAD using &quot;</span>);</div>
<div class="line">            <span class="comment">/* NOTE:</span></div>
<div class="line"><span class="comment">             *  MQTT - json_doc holds the MQTT JSON request document.</span></div>
<div class="line"><span class="comment">             *  HTTP - json_doc holds the HTTP &quot;GET&quot; request.</span></div>
<div class="line"><span class="comment">             */</span></div>
<div class="line">            <span class="keywordflow">if</span> (cb_data-&gt;<a class="code" href="structcy__ota__cb__struct__t.html#aabc069ca938caaeda44300df68d31903">connection_type</a> == <a class="code" href="group__group__ota__typedefs.html#ggabc03646427dec963bef37cb5dbb550faa7955097dd62d0b81cb447d287d12bd98">CY_OTA_CONNECTION_MQTT</a>)</div>
<div class="line">            {</div>
<div class="line">                printf(<span class="stringliteral">&quot;MQTT: &#39;%s&#39; \n&quot;</span>, cb_data-&gt;json_doc);</div>
<div class="line">                printf(<span class="stringliteral">&quot;                       topic: &#39;%s&#39; \n&quot;</span>, cb_data-&gt;unique_topic);</div>
<div class="line">            }</div>
<div class="line">            <span class="keywordflow">else</span></div>
<div class="line">            {</div>
<div class="line">                printf(<span class="stringliteral">&quot;HTTP: &#39;%s&#39; \n&quot;</span>, cb_data-&gt;json_doc);</div>
<div class="line">                printf(<span class="stringliteral">&quot;                        file: &#39;%s&#39; \n&quot;</span>, cb_data-&gt;file);</div>
<div class="line">            }</div>
<div class="line">            <span class="keywordflow">break</span>;</div>
<div class="line">        <span class="keywordflow">case</span> <a class="code" href="group__group__ota__typedefs.html#ggab1539ebc23219f1ca64318286e9d375fa1957660855d0051d85c9c6bc52bee808">CY_OTA_STATE_DATA_DISCONNECT</a>:</div>
<div class="line">            printf(<span class="stringliteral">&quot;APP CB OTA DATA DISCONNECT\n&quot;</span>);</div>
<div class="line">            <span class="keywordflow">break</span>;</div>
<div class="line"></div>
<div class="line">        <span class="keywordflow">case</span> <a class="code" href="group__group__ota__typedefs.html#ggab1539ebc23219f1ca64318286e9d375fa64f1d70db6ac1c7b6053aa72a829cfd7">CY_OTA_STATE_VERIFY</a>:</div>
<div class="line">            printf(<span class="stringliteral">&quot;APP CB OTA VERIFY\n&quot;</span>);</div>
<div class="line">            <span class="keywordflow">break</span>;</div>
<div class="line"></div>
<div class="line">        <span class="keywordflow">case</span> <a class="code" href="group__group__ota__typedefs.html#ggab1539ebc23219f1ca64318286e9d375fa155daa564765a5efdb0f5e647832c25b">CY_OTA_STATE_RESULT_REDIRECT</a>:</div>
<div class="line">            printf(<span class="stringliteral">&quot;APP CB OTA RESULT REDIRECT\n&quot;</span>);</div>
<div class="line">            <span class="keywordflow">break</span>;</div>
<div class="line"></div>
<div class="line">        <span class="keywordflow">case</span> <a class="code" href="group__group__ota__typedefs.html#ggab1539ebc23219f1ca64318286e9d375fa32bb3fbef7ed0e85e1130a8f8c206ac2">CY_OTA_STATE_RESULT_CONNECT</a>:</div>
<div class="line">            printf(<span class="stringliteral">&quot;APP CB OTA SEND RESULT CONNECT using &quot;</span>);</div>
<div class="line">            <span class="comment">/* NOTE:</span></div>
<div class="line"><span class="comment">             *  MQTT - json_doc holds the MQTT JSON request document.</span></div>
<div class="line"><span class="comment">             *  HTTP - json_doc holds the HTTP &quot;GET&quot; request.</span></div>
<div class="line"><span class="comment">             */</span></div>
<div class="line">            <span class="keywordflow">if</span> (cb_data-&gt;<a class="code" href="structcy__ota__cb__struct__t.html#aabc069ca938caaeda44300df68d31903">connection_type</a> == <a class="code" href="group__group__ota__typedefs.html#ggabc03646427dec963bef37cb5dbb550faa7955097dd62d0b81cb447d287d12bd98">CY_OTA_CONNECTION_MQTT</a>)</div>
<div class="line">            {</div>
<div class="line">                printf(<span class="stringliteral">&quot;MQTT: Broker:%s port: %d\n&quot;</span>, cb_data-&gt;broker_server.pHostName, cb_data-&gt;broker_server.port);</div>
<div class="line">                printf(<span class="stringliteral">&quot;                                      topic: &#39;%s&#39; \n&quot;</span>, cb_data-&gt;unique_topic);</div>
<div class="line">            }</div>
<div class="line">            <span class="keywordflow">else</span></div>
<div class="line">            {</div>
<div class="line">                printf(<span class="stringliteral">&quot;HTTP: Server:%s port: %d\n&quot;</span>, cb_data-&gt;broker_server.pHostName, cb_data-&gt;broker_server.port);</div>
<div class="line">            }</div>
<div class="line">            <span class="keywordflow">break</span>;</div>
<div class="line">        <span class="keywordflow">case</span> <a class="code" href="group__group__ota__typedefs.html#ggab1539ebc23219f1ca64318286e9d375fa38c70df41f2fab401d96e10bc21cb395">CY_OTA_STATE_RESULT_SEND</a>:</div>
<div class="line">            printf(<span class="stringliteral">&quot;APP CB OTA SENDING RESULT using &quot;</span>);</div>
<div class="line">            <span class="comment">/* NOTE:</span></div>
<div class="line"><span class="comment">             *  MQTT - json_doc holds the MQTT JSON request document.</span></div>
<div class="line"><span class="comment">             *  HTTP - json_doc holds the HTTP &quot;PUT&quot;.</span></div>
<div class="line"><span class="comment">             */</span></div>
<div class="line">            <span class="keywordflow">if</span> (cb_data-&gt;<a class="code" href="structcy__ota__cb__struct__t.html#aabc069ca938caaeda44300df68d31903">connection_type</a> == <a class="code" href="group__group__ota__typedefs.html#ggabc03646427dec963bef37cb5dbb550faa7955097dd62d0b81cb447d287d12bd98">CY_OTA_CONNECTION_MQTT</a>)</div>
<div class="line">            {</div>
<div class="line">                printf(<span class="stringliteral">&quot;MQTT: &#39;%s&#39; \n&quot;</span>, cb_data-&gt;json_doc);</div>
<div class="line">            }</div>
<div class="line">            <span class="keywordflow">else</span></div>
<div class="line">            {</div>
<div class="line">                printf(<span class="stringliteral">&quot;HTTP: &#39;%s&#39; \n&quot;</span>, cb_data-&gt;json_doc);</div>
<div class="line">            }</div>
<div class="line">            <span class="keywordflow">break</span>;</div>
<div class="line">        <span class="keywordflow">case</span> <a class="code" href="group__group__ota__typedefs.html#ggab1539ebc23219f1ca64318286e9d375fa559e81062c0a2f47f03dcbffefbc7a75">CY_OTA_STATE_RESULT_RESPONSE</a>:</div>
<div class="line">            printf(<span class="stringliteral">&quot;APP CB OTA Got Result response\n&quot;</span>);</div>
<div class="line">            <span class="keywordflow">break</span>;</div>
<div class="line"></div>
<div class="line">        <span class="keywordflow">case</span> <a class="code" href="group__group__ota__typedefs.html#ggab1539ebc23219f1ca64318286e9d375fa7608a11942fc4b42c3487335639fd719">CY_OTA_STATE_RESULT_DISCONNECT</a>:</div>
<div class="line">            printf(<span class="stringliteral">&quot;APP CB OTA Result Disconnect\n&quot;</span>);</div>
<div class="line">            <span class="keywordflow">break</span>;</div>
<div class="line"></div>
<div class="line">        <span class="keywordflow">case</span> <a class="code" href="group__group__ota__typedefs.html#ggab1539ebc23219f1ca64318286e9d375fa1a0a36230c7f92d5b5af2394e14fa6a1">CY_OTA_STATE_OTA_COMPLETE</a>:</div>
<div class="line">            printf(<span class="stringliteral">&quot;APP CB OTA Session Complete\n&quot;</span>);</div>
<div class="line">            <span class="keywordflow">break</span>;</div>
<div class="line"></div>
<div class="line">        <span class="keywordflow">case</span> <a class="code" href="group__group__ota__typedefs.html#ggab1539ebc23219f1ca64318286e9d375faabbd5e53bc741afd226adfd143dde572">CY_OTA_NUM_STATES</a>: <span class="comment">/* To keep compiler happy. */</span></div>
<div class="line">            <span class="keywordflow">break</span>;</div>
<div class="line"></div>
<div class="line">        }   <span class="comment">/* Switch state. */</span></div>
<div class="line">        <span class="keywordflow">break</span>;</div>
<div class="line"></div>
<div class="line"></div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    <span class="keywordflow">return</span> cb_result;</div>
<div class="line">}</div>
<div class="line"></div>
</div><!-- fragment --><p> <br />
 <b>OTA Initialization:</b><br />
 The following code snippet demonstrates the initialization of the OTA Agent.<br />
 </p><div class="fragment"><div class="line"><span class="comment">/* Initialize and start the OTA Agent.</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> * The main application must:</span></div>
<div class="line"><span class="comment"> * - In case of XMC7200 devices, disables and invalidate instruction cache and disable, clean and invalidate data cache.</span></div>
<div class="line"><span class="comment"> *          SCB_DisableICache(); // Disables and invalidates instruction cache</span></div>
<div class="line"><span class="comment"> *          SCB_DisableDCache(); // Disables, cleans and invalidates data cache</span></div>
<div class="line"><span class="comment"> * - In case of CAT1C devices, like XMC7200 devices, initializa appropriate flash access apis.</span></div>
<div class="line"><span class="comment"> *          Cy_Flash_Init();</span></div>
<div class="line"><span class="comment"> *          Cy_Flashc_MainWriteEnable();</span></div>
<div class="line"><span class="comment"> *          Cy_Flashc_WorkWriteEnable();</span></div>
<div class="line"><span class="comment"> * - Turn off the Watchdog Timer (if being used)</span></div>
<div class="line"><span class="comment"> *          cyhal_wdt_t wdt_obj;</span></div>
<div class="line"><span class="comment"> *          cyhal_wdt_init(&amp;wdt_obj, cyhal_wdt_get_max_timeout_ms());</span></div>
<div class="line"><span class="comment"> *          cyhal_wdt_free(&amp;wdt_obj);</span></div>
<div class="line"><span class="comment"> * - Validate the application (if the update just happened)</span></div>
<div class="line"><span class="comment"> *          cy_ota_storage_validated();</span></div>
<div class="line"><span class="comment"> * - In case of Wi-Fi enabled devices like PSoC62, PSoC63 or PSoC64 :</span></div>
<div class="line"><span class="comment"> *          Initialize Wi-Fi.</span></div>
<div class="line"><span class="comment"> *          Connect to the Wi-Fi AP.</span></div>
<div class="line"><span class="comment"> * - In case of Ethernet enabled devices like XMC7200 :</span></div>
<div class="line"><span class="comment"> *          Initialize Ethernet Module.</span></div>
<div class="line"><span class="comment"> *          Bring up the interface and configure to corresponding Ethernet port.</span></div>
<div class="line"><span class="comment"> * </span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"></div>
<div class="line">cy_rslt_t ota_setup(<span class="keywordtype">void</span>)</div>
<div class="line">{</div>
<div class="line">    cy_rslt_t   result;</div>
<div class="line"></div>
<div class="line">    <span class="comment">/*****************************************************************************</span></div>
<div class="line"><span class="comment">    &lt; Add code snippet here to initialize and connect to a Wi-Fi AP &gt;</span></div>
<div class="line"><span class="comment">    ******************************************************************************/</span></div>
<div class="line"></div>
<div class="line">    <span class="comment">/* Initialize the underlying support code that is needed for OTA and MQTT. */</span></div>
<div class="line">    <span class="keywordflow">if</span> (cy_awsport_network_init() != CY_RSLT_SUCCESS)</div>
<div class="line">    {</div>
<div class="line">        printf(<span class="stringliteral">&quot;cy_awsport_network_init Failed.\n&quot;</span>);</div>
<div class="line">        <span class="keywordflow">while</span>(1)</div>
<div class="line">        {</div>
<div class="line">            cy_rtos_delay_milliseconds(10);</div>
<div class="line">        }</div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    <span class="comment">/* Initialize the MQTT subsystem. */</span></div>
<div class="line">    <span class="keywordflow">if</span> (cy_mqtt_init() != CY_RSLT_SUCCESS )</div>
<div class="line">    {</div>
<div class="line">        printf(<span class="stringliteral">&quot;cy_mqtt_init() Failed.\n&quot;</span>);</div>
<div class="line">        <span class="keywordflow">while</span>(1)</div>
<div class="line">        {</div>
<div class="line">            cy_rtos_delay_milliseconds(10);</div>
<div class="line">        }</div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">     <span class="comment">/* Set up parameters for starting the OTA Agent. */</span></div>
<div class="line">     memset(&amp;network_params, 0, <span class="keyword">sizeof</span>(network_params));</div>
<div class="line">     memset(&amp;agent_params,   0, <span class="keyword">sizeof</span>(agent_params));</div>
<div class="line"></div>
<div class="line">     <span class="comment">/* Common network parameters. */</span></div>
<div class="line">     <span class="comment">/* Server parameters. */</span></div>
<div class="line">     network_params.<a class="code" href="structcy__ota__network__params__t.html#abcc290be8049d49f332165ac7aafb3e0">initial_connection</a> = ota-&gt;connection_type;</div>
<div class="line"></div>
<div class="line">     <span class="comment">/* Set all network parameters for both MQTT &amp; HTTP in case of switching</span></div>
<div class="line"><span class="comment">      * from Job Broker/server to data Broker/server.</span></div>
<div class="line"><span class="comment">      */</span></div>
<div class="line">     <span class="comment">/* MQTT Connection values. */</span></div>
<div class="line">     network_params.mqtt.broker.host_name = MQTT_BROKER;</div>
<div class="line">     network_params.mqtt.broker.port = MQTT_BROKER_PORT;</div>
<div class="line"></div>
<div class="line">     network_params.<a class="code" href="structcy__ota__network__params__t.html#a5668e310a824240b5b756b23f63095c0">use_get_job_flow</a>     = <span class="keyword">true</span>;                    <span class="comment">/* False for Direct flow. */</span></div>
<div class="line">     network_params.mqtt.numTopicFilters = MQTT_TOPIC_FILTER_NUM;</div>
<div class="line">     network_params.mqtt.pTopicFilters   = mqtt_topics;</div>
<div class="line">     network_params.mqtt.pIdentifier     = <span class="stringliteral">&quot;MQTTClientUniqueIdentifier&quot;</span>;    <span class="comment">/* Create a unique identifier. */</span></div>
<div class="line">     network_params.mqtt.session_type    = <a class="code" href="group__group__ota__typedefs.html#gga6424608a6e19ecd99e52f4013354fa82a63a3e6f75f26a0026b89674544f8c162">CY_OTA_MQTT_SESSION_CLEAN</a>;</div>
<div class="line"></div>
<div class="line">     <span class="comment">/* HTTP connection values. */</span></div>
<div class="line">     network_params.http.server.host_name = HTTP_SERVER;      <span class="comment">/* Must not be a local variable. */</span></div>
<div class="line">     network_params.http.server.port = HTTP_SERVER_PORT;</div>
<div class="line"></div>
<div class="line">     <span class="comment">/* For HTTP, change the file with the GET when using a Job flow. */</span></div>
<div class="line">     network_params.<a class="code" href="structcy__ota__network__params__t.html#a5668e310a824240b5b756b23f63095c0">use_get_job_flow</a> = ota-&gt;update_flow;</div>
<div class="line">     <span class="keywordflow">if</span> (network_params.<a class="code" href="structcy__ota__network__params__t.html#a5668e310a824240b5b756b23f63095c0">use_get_job_flow</a> == <a class="code" href="group__group__ota__typedefs.html#ggaa14519d9111acc0f50e9fd5307283766aa4d7311e721aa652fb94feda759e316c">CY_OTA_JOB_FLOW</a>)</div>
<div class="line">     {</div>
<div class="line">         <span class="comment">/* For HTTP server to get the &quot;Job&quot; file. */</span></div>
<div class="line">         network_params.http.file = OTA_HTTP_JOB_FILE;</div>
<div class="line">     }</div>
<div class="line">     <span class="keywordflow">else</span></div>
<div class="line">     {</div>
<div class="line">         <span class="comment">/* For HTTP server to get the &quot;data&quot; file directly. */</span></div>
<div class="line">         network_params.http.file = OTA_HTTP_DATA_FILE;</div>
<div class="line">     }</div>
<div class="line"></div>
<div class="line">     <span class="comment">/* Set up MQTT credentials - only used if start_TLS is set. */</span></div>
<div class="line">     memset(&amp;network_params.mqtt.credentials, 0x00, <span class="keyword">sizeof</span>(network_params.mqtt.credentials) );</div>
<div class="line"></div>
<div class="line">     <span class="comment">/* if service is AMAZON, for testing we need to set the username */</span></div>
<div class="line">     <span class="keywordflow">if</span> (ota-&gt;mqtt_certificates == OTA_MQTT_SERVICE_AMAZON)</div>
<div class="line">     {</div>
<div class="line">         <span class="comment">/* Only set this for Amazon testing. */</span></div>
<div class="line">         mqtt_credentials.username      = <span class="stringliteral">&quot;Test&quot;</span>;</div>
<div class="line">         mqtt_credentials.username_size = strlen(mqtt_credentials.username);</div>
<div class="line">         mqtt_credentials.password      = <span class="stringliteral">&quot;&quot;</span>;</div>
<div class="line">         mqtt_credentials.password_size = strlen(mqtt_credentials.password);</div>
<div class="line">     }</div>
<div class="line">     mqtt_credentials.alpnprotos         = NULL;</div>
<div class="line">     mqtt_credentials.alpnprotoslen      = 0;</div>
<div class="line">     mqtt_credentials.sni_host_name      = NULL;</div>
<div class="line">     mqtt_credentials.sni_host_name_size = 0;</div>
<div class="line">     mqtt_credentials.root_ca            = mqtt_root_ca_certificate_table[ota-&gt;mqtt_certificates];</div>
<div class="line">     mqtt_credentials.root_ca_size       = strlen(mqtt_root_ca_certificate_table[ota-&gt;mqtt_certificates]) + 1;</div>
<div class="line">     mqtt_credentials.client_cert        = mqtt_client_cert_table[ota-&gt;mqtt_certificates];</div>
<div class="line">     mqtt_credentials.client_cert_size   = strlen(mqtt_client_cert_table[ota-&gt;mqtt_certificates]) + 1;</div>
<div class="line">     mqtt_credentials.private_key        = mqtt_client_key_table[ota-&gt;mqtt_certificates];</div>
<div class="line">     mqtt_credentials.private_key_size   = strlen(mqtt_client_key_table[ota-&gt;mqtt_certificates]) + 1;</div>
<div class="line">     <span class="keywordflow">if</span> (ota-&gt;start_TLS)</div>
<div class="line">     {</div>
<div class="line">         network_params.mqtt.credentials = mqtt_credentials;</div>
<div class="line">     }</div>
<div class="line"></div>
<div class="line">     memset(&amp;network_params.http.credentials, 0x00, <span class="keyword">sizeof</span>(network_params.http.credentials ));</div>
<div class="line"></div>
<div class="line">     <span class="comment">/* Set up HTTP credentials - only used if start_TLS is set. */</span></div>
<div class="line"></div>
<div class="line">     <span class="comment">/* Only if the HTTP server needs this: */</span></div>
<div class="line">     <span class="comment">//http_credentials.password           = &quot;&quot;;</span></div>
<div class="line">     <span class="comment">//http_credentials.username_size      = strlen(http_credentials.password);</span></div>
<div class="line">     <span class="comment">//http_credentials.password           = &quot;&quot;;</span></div>
<div class="line">     <span class="comment">//http_credentials.password_size      = strlen(http_credentials.password);</span></div>
<div class="line">     http_credentials.alpnprotos         = NULL;</div>
<div class="line">     http_credentials.alpnprotoslen      = 0;</div>
<div class="line">     http_credentials.sni_host_name      = NULL;</div>
<div class="line">     http_credentials.sni_host_name_size = 0;</div>
<div class="line">     http_credentials.root_ca            = (<span class="keyword">const</span> <span class="keywordtype">char</span> *)root_ca_certificate_https;</div>
<div class="line">     http_credentials.root_ca_size       = strlen(root_ca_certificate_https) + 1;</div>
<div class="line">     http_credentials.client_cert        = (<span class="keyword">const</span> <span class="keywordtype">char</span> *)client_cert_https;</div>
<div class="line">     http_credentials.client_cert_size   = strlen(client_cert_https) + 1;</div>
<div class="line">     http_credentials.private_key        = (<span class="keyword">const</span> <span class="keywordtype">char</span> *)client_key_https;</div>
<div class="line">     http_credentials.private_key_size   = strlen(client_key_https) + 1;</div>
<div class="line">     <span class="keywordflow">if</span> (ota-&gt;start_TLS)</div>
<div class="line">     {</div>
<div class="line">         network_params.http.credentials = http_credentials;</div>
<div class="line">     }</div>
<div class="line"></div>
<div class="line">     <span class="comment">/* OTA Agent parameters. */</span></div>
<div class="line">     agent_params.validate_after_reboot  = 0;        <span class="comment">/* Validate after download so that you don&#39;t have to call</span></div>
<div class="line"><span class="comment">                                                      *          cy_ota_validated() on reboot.               */</span></div>
<div class="line">     agent_params.reboot_upon_completion = 1;        <span class="comment">/* 1 = Reboot when download is finished.                */</span></div>
<div class="line">     agent_params.do_not_send_result     = 0;        <span class="comment">/* 1 = Do not send the result info to the Broker/server.        */</span></div>
<div class="line">     agent_params.cb_func                = ota_callback;</div>
<div class="line">     agent_params.cb_arg                 = ota;</div>
<div class="line"></div>
<div class="line">    <span class="comment">/* Initialize and start the OTA Agent. */</span></div>
<div class="line">    result = <a class="code" href="group__group__ota__functions.html#ga05b1ce8823a81d534247afd43db4c536">cy_ota_agent_start</a>(&amp;network_params, &amp;agent_params, &amp;ota_interfaces, &amp;ota_context);</div>
<div class="line"></div>
<div class="line">    <span class="keywordflow">return</span> result;</div>
<div class="line">}</div>
</div><!-- fragment --><p> <br />
 <b>Application Bluetooth® initialization:</b><br />
 The following code snippet demonstrates and example for initializing the Bluetooth® Stack required before starting the OTA Agent. The next snippet shows the Bluetooth® GATT callback handler.<br />
 </p><div class="fragment"><div class="line"><span class="comment">/*</span></div>
<div class="line"><span class="comment"> * Function name:</span></div>
<div class="line"><span class="comment"> * bt_app_init</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> * Function Description:</span></div>
<div class="line"><span class="comment"> * @brief    This function is executed if BTM_ENABLED_EVT event occurs in</span></div>
<div class="line"><span class="comment"> *           Bluetooth® management callback.</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> * @param    void</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> * @return    void</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> bt_app_init(<span class="keywordtype">void</span>)</div>
<div class="line">{</div>
<div class="line">    wiced_result_t  wiced_result;</div>
<div class="line">    wiced_bt_gatt_status_t status = WICED_BT_GATT_BUSY;</div>
<div class="line"></div>
<div class="line">    <span class="comment">/* Initialising the HCI UART for Host contol */</span></div>
<div class="line">    cybt_platform_config_init(&amp;app_bt_platform_cfg_settings);</div>
<div class="line"></div>
<div class="line">    <span class="comment">/* Register with stack to receive GATT callback */</span></div>
<div class="line">    status = wiced_bt_gatt_register(app_bt_gatt_event_handler);</div>
<div class="line">    printf(<span class="stringliteral">&quot;wiced_bt_gatt_register() status (0x%lx) %s\n&quot;</span>, status, app_get_gatt_status_name(status));</div>
<div class="line"></div>
<div class="line">    <span class="comment">/* Initialize GATT Database */</span></div>
<div class="line">    status = wiced_bt_gatt_db_init(gatt_database, gatt_database_len, NULL);</div>
<div class="line">    <span class="keywordflow">if</span>(status != WICED_BT_GATT_SUCCESS)</div>
<div class="line">    {</div>
<div class="line">        printf(<span class="stringliteral">&quot;%s() wiced_bt_gatt_db_init() FAILED 0x%lx !\n&quot;</span>, __func__, status);</div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    <span class="comment">/* Allow peer to pair */</span></div>
<div class="line">    wiced_bt_set_pairable_mode(WICED_TRUE, 0);</div>
<div class="line"></div>
<div class="line">    <span class="comment">/* Set Advertisement Data */</span></div>
<div class="line">    status = app_bt_set_advertisement_data();</div>
<div class="line">    <span class="keywordflow">if</span>(status != WICED_SUCCESS)</div>
<div class="line">    {</div>
<div class="line">        printf(<span class="stringliteral">&quot;%s() app_bt_set_advertisement_data() FAILED 0x%lx !\n&quot;</span>, __func__, status);</div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    <span class="comment">/* Start Undirected LE Advertisements on device startup. */</span></div>
<div class="line">    status = wiced_bt_start_advertisements(BTM_BLE_ADVERT_UNDIRECTED_HIGH, BLE_ADDR_PUBLIC, NULL);</div>
<div class="line">    <span class="keywordflow">if</span>(status != WICED_SUCCESS)</div>
<div class="line">    {</div>
<div class="line">        printf(<span class="stringliteral">&quot;%s() wiced_bt_start_advertisements()  FAILED 0x%lx\n&quot;</span>, __func__, status);</div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    <span class="comment">/* Register call back and configuration with stack */</span></div>
<div class="line">    wiced_result = wiced_bt_stack_init(app_bt_management_callback , &amp;wiced_bt_cfg_settings);</div>
<div class="line">    <span class="keywordflow">if</span>( WICED_BT_SUCCESS == wiced_result)</div>
<div class="line">    {</div>
<div class="line">        printf(<span class="stringliteral">&quot;Bluetooth(r) Stack Initialization Successful \n&quot;</span>);</div>
<div class="line">    }</div>
<div class="line">    <span class="keywordflow">else</span></div>
<div class="line">    {</div>
<div class="line">        printf(<span class="stringliteral">&quot;Bluetooth(r) Stack Initialization failed!!\n&quot;</span>);</div>
<div class="line">    }</div>
<div class="line">}</div>
</div><!-- fragment --><p> <br />
 <b>Application Bluetooth® Callback Handling</b>:<br />
 The following code snippet demonstrates and example for initializing the Bluetooth® Stack required before starting the OTA Agent.<br />
 </p><div class="fragment"><div class="line"><span class="comment">/*</span></div>
<div class="line"><span class="comment"> * Function Name:</span></div>
<div class="line"><span class="comment"> * app_bt_gatt_event_handler</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> * Function Description:</span></div>
<div class="line"><span class="comment"> * @brief  This Function handles the all the GATT events - GATT Event Handler</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> * @param event            Bluetooth® GATT event type</span></div>
<div class="line"><span class="comment"> * @param p_event_data     Pointer to Bluetooth® GATT event data</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> * @return wiced_bt_gatt_status_t  Bluetooth® GATT status</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"><span class="keyword">static</span> wiced_bt_gatt_status_t app_bt_gatt_event_handler(wiced_bt_gatt_evt_t event, wiced_bt_gatt_event_data_t *p_event_data)</div>
<div class="line">{</div>
<div class="line"></div>
<div class="line">    wiced_bt_gatt_status_t status = WICED_BT_GATT_SUCCESS;</div>
<div class="line">    wiced_bt_gatt_attribute_request_t *p_attr_req = &amp;p_event_data-&gt;attribute_request;</div>
<div class="line"></div>
<div class="line">    <span class="keywordflow">switch</span> (event)</div>
<div class="line">    {</div>
<div class="line"></div>
<div class="line">    <span class="keywordflow">case</span> GATT_CONNECTION_STATUS_EVT:    <span class="comment">/* GATT connection status change. Event data: #wiced_bt_gatt_connection_status_t */</span></div>
<div class="line">        printf(<span class="stringliteral">&quot;\n\n%s() GATT_CONNECTION_STATUS_EVT:  %d\n&quot;</span>, __func__, event);</div>
<div class="line">        status = app_bt_connect_callback(&amp;p_event_data-&gt;connection_status);</div>
<div class="line">        <span class="keywordflow">break</span>;</div>
<div class="line"></div>
<div class="line">    <span class="keywordflow">case</span> GATT_ATTRIBUTE_REQUEST_EVT:    <span class="comment">/* GATT attribute request (from remote client). Event data: #wiced_bt_gatt_attribute_request_t */</span></div>
<div class="line">        printf(<span class="stringliteral">&quot;\n\n%s() GATT_ATTRIBUTE_REQUEST_EVT:  %d type:%d\n&quot;</span>, __func__, event, p_attr_req-&gt;opcode);</div>
<div class="line">        status = app_bt_server_callback(p_event_data);</div>
<div class="line">        <span class="keywordflow">break</span>;</div>
<div class="line"></div>
<div class="line">    <span class="keywordflow">case</span> GATT_GET_RESPONSE_BUFFER_EVT:      <span class="comment">/* GATT buffer request, typically sized to max of bearer mtu - 1 */</span></div>
<div class="line">        printf(<span class="stringliteral">&quot;\n\n%s() GATT_GET_RESPONSE_BUFFER_EVT\n&quot;</span>, __func__);</div>
<div class="line">        p_event_data-&gt;buffer_request.buffer.p_app_rsp_buffer = app_bt_alloc_buffer (p_event_data-&gt;buffer_request.len_requested);</div>
<div class="line">        p_event_data-&gt;buffer_request.buffer.p_app_ctxt       = (<span class="keywordtype">void</span>*)app_bt_free_buffer;</div>
<div class="line">        status = WICED_BT_GATT_SUCCESS;</div>
<div class="line">        <span class="keywordflow">break</span>;</div>
<div class="line"></div>
<div class="line">    <span class="keywordflow">case</span> GATT_APP_BUFFER_TRANSMITTED_EVT:    <span class="comment">/* GATT buffer transmitted event,  check \ref wiced_bt_gatt_buffer_transmitted_t*/</span></div>
<div class="line">        printf(<span class="stringliteral">&quot;\n\n%s() GATT_APP_BUFFER_TRANSMITTED_EVT.\n&quot;</span>, __func__);</div>
<div class="line">        {</div>
<div class="line">            pfn_free_buffer_t pfn_free = (pfn_free_buffer_t)p_event_data-&gt;buffer_xmitted.p_app_ctxt;</div>
<div class="line"></div>
<div class="line">            <span class="comment">/* If the buffer is dynamic, the context will point to a function to free it. */</span></div>
<div class="line">            if (pfn_free)</div>
<div class="line">                pfn_free(p_event_data-&gt;buffer_xmitted.p_app_data);</div>
<div class="line"></div>
<div class="line">            status = WICED_BT_GATT_SUCCESS;</div>
<div class="line">        }</div>
<div class="line">        <span class="keywordflow">break</span>;</div>
<div class="line"></div>
<div class="line">    <span class="keywordflow">case</span> GATT_OPERATION_CPLT_EVT:       <span class="comment">/* GATT operation complete. Event data: #wiced_bt_gatt_event_data_t */</span></div>
<div class="line">        printf(<span class="stringliteral">&quot;\n\n%s() GATT_OPERATION_CPLT_EVT:  We are a server, nothing to do.\n&quot;</span>, __func__);</div>
<div class="line">        <span class="keywordflow">break</span>;</div>
<div class="line"></div>
<div class="line">    <span class="keywordflow">default</span>:</div>
<div class="line">        printf(<span class="stringliteral">&quot;\n\n%s()------------------&gt; Unhandled GATT event: %d\n\n&quot;</span>, __func__, event);</div>
<div class="line">        status = WICED_BT_GATT_SUCCESS;</div>
<div class="line">        <span class="keywordflow">break</span>;</div>
<div class="line"></div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    <span class="keywordflow">return</span> status;</div>
<div class="line">}</div>
</div><!-- fragment --><p> <br />
 <b>OTA Agent Bluetooth® initialization:</b><br />
 The following code snippet demonstrates starting the OTA Agent for use with Bluetooth®.<br />
 </p><div class="fragment"><div class="line"><span class="comment">/* Initialize and start the OTA Agent.</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> * The main application must:</span></div>
<div class="line"><span class="comment"> * - Turn off the Watchdog Timer (if being used)</span></div>
<div class="line"><span class="comment"> *          cyhal_wdt_t wdt_obj;</span></div>
<div class="line"><span class="comment"> *          cyhal_wdt_init(&amp;wdt_obj, cyhal_wdt_get_max_timeout_ms());</span></div>
<div class="line"><span class="comment"> *          cyhal_wdt_free(&amp;wdt_obj);</span></div>
<div class="line"><span class="comment"> * - Validate the application (if the update just happened)</span></div>
<div class="line"><span class="comment"> *          cy_ota_storage_validated();</span></div>
<div class="line"><span class="comment"> * - Initialize Bluetooth®.</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> * */</span></div>
<div class="line"></div>
<div class="line"><span class="comment">/* Network parameters for OTA Agent. */</span></div>
<div class="line"><a class="code" href="structcy__ota__network__params__t.html">cy_ota_network_params_t</a>     network_params;</div>
<div class="line"></div>
<div class="line"><span class="comment">/* Parameters for the OTA Agent. */</span></div>
<div class="line"><a class="code" href="structcy__ota__agent__params__t.html">cy_ota_agent_params_t</a> ota_agent_params;</div>
<div class="line"></div>
<div class="line"><span class="comment">/* OTA Agent context pointer storage. */</span></div>
<div class="line"><a class="code" href="group__group__ota__typedefs.html#ga50b8b1ec191a1d5f9a8fcd3472811694">cy_ota_context_ptr</a> ota_context;</div>
<div class="line"></div>
<div class="line">cy_rslt_t ota_setup(<span class="keywordtype">void</span>)</div>
<div class="line">{</div>
<div class="line">    cy_rslt_t   result;</div>
<div class="line"></div>
<div class="line">    <span class="comment">/*****************************************************************************</span></div>
<div class="line"><span class="comment">    * This is called from the Bluetooth® callback</span></div>
<div class="line"><span class="comment">    *   Handle : HDLC_OTA_FW_UPGRADE_SERVICE_OTA_UPGRADE_CONTROL_POINT_VALUE</span></div>
<div class="line"><span class="comment">    *   Command: CY_OTA_UPGRADE_COMMAND_PREPARE_DOWNLOAD</span></div>
<div class="line"><span class="comment">    ******************************************************************************/</span></div>
<div class="line"></div>
<div class="line">     <span class="comment">/* Set up parameters for starting the OTA Agent. */</span></div>
<div class="line">     memset(&amp;network_params, 0, <span class="keyword">sizeof</span>(network_params));</div>
<div class="line">     memset(&amp;agent_params,   0, <span class="keyword">sizeof</span>(agent_params));</div>
<div class="line"></div>
<div class="line">     <span class="comment">/* Common network parameters. */</span></div>
<div class="line">     <span class="comment">/* Server parameters. */</span></div>
<div class="line">     network_params.<a class="code" href="structcy__ota__network__params__t.html#abcc290be8049d49f332165ac7aafb3e0">initial_connection</a> = <a class="code" href="group__group__ota__typedefs.html#ggabc03646427dec963bef37cb5dbb550faaa6e2c528481c3eb950755dd86dfb272f">CY_OTA_CONNECTION_BLE</a>;</div>
<div class="line"></div>
<div class="line">    <span class="comment">/* Initialize and start the OTA Agent. */</span></div>
<div class="line">    result = <a class="code" href="group__group__ota__functions.html#ga05b1ce8823a81d534247afd43db4c536">cy_ota_agent_start</a>(&amp;network_params, &amp;agent_params, &amp;ota_interfaces, &amp;ota_context);</div>
<div class="line"></div>
<div class="line">    <span class="keywordflow">return</span> result;</div>
<div class="line">}</div>
</div><!-- fragment --> <h1><a class="anchor" id="section_example"></a>
ModusToolbox OTA Code Examples</h1>
<p>ModusToolbox OTA Example using MQTT: <a href="https://github.com/infineon/mtb-example-ota-mqtt">https://github.com/infineon/mtb-example-ota-mqtt</a><br />
 ModusToolbox OTA Example using HTTP: <a href="https://github.com/Infineon/mtb-example-ota-https">https://github.com/Infineon/mtb-example-ota-https</a><br />
 ModusToolbox OTA Update Bluetooth® example: <a href="https://github.com/infineon/mtb-example-btstack-freertos-cyw20829-keyboard">https://github.com/infineon/mtb-example-btstack-freertos-cyw20829-keyboard</a><br />
 ModusToolbox OTA Update Bluetooth® example: <a href="https://github.com/Infineon/mtb-example-btstack-freertos-cyw20829-battery-server-ota">https://github.com/Infineon/mtb-example-btstack-freertos-cyw20829-battery-server-ota</a><br />
</p>
</div></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath">
    <ul>
        <li class="footer">
            Generated for <b>Over The Air (OTA) Update Library</b> by <b>Cypress Semiconductor Corporation</b>.
            All rights reserved.
        </li>
    </ul>
</div>
</body>
</html>
